About the Demo

There are two main purposes of this demo
  1. The primary purpose of the example is to show how to perform the conversion of image data from byte arrays to Image objects so that they can be displayed in standard Winforms PictureBoxes.
  2. The secondary purpose is to give the fishes in the good old FishFacts demo safe harbour in a brand new ECO-logical world.

Setup Information

The demo uses the DbDemos database that ships with Delphi. Make sure the connection string in the BdpConnection refers to your DbDemos database (should be located in $(BDS)\Demos\Examples\Data\DbDemos.ib)

Points of interest

The interesting details in this demo are located in WinForm.cs.

In the constructor, just after the components have been initialized, a new databinding is created manually. This databinding is equipped with a Format and a Parse event. The Format event is used to convert the binary image data from System.Byte[] (byte array) to an image object. The parse event is used to perform the reverse conversion. The new databinding is assigned to the PictureBox.

The Format event (ByteArrayWithHeaderToImage) converts the byte array to an image by first stuffing the bytes into a MemoryStream. Since the DbDemos database has an 8 byte header in each blob, this data is stripped away. The memory stream is finally passed as a parameter to the constructor of the Bitmap class (descendant from Image class)

The Parse event (ImageToByteArrayWithHeader) receives as input an image object. This image object is saved to a stream. In the stream we also have to write the header (that we stripped away in the Format event) so that we don't break the data format of the database. Once the correct data is in the stream we convert the stream to a byte array.

The Databinding does not keep track of if the image has changed or not. When the grid changes active row, the databinding will try to parse the image and compare to the value stored in the BioLife.Graphic attribute. Since the parse method does not return the same byte array as was originally in the attribute, the object will become dirty. For this reason, the picture box has been set to readonly. The parse event is still called (for unclear reasons), but the value is never written to the underlying attribute.